﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using BeeZ.CommonObjects.Interfaces;
using BeeZ.CommonObjects.Honey;

using System.Diagnostics;

namespace BeeZ.Client
{

    namespace Bees
    {

        public class IdleBee : AbstractBee
        {
            private int idleCriteria = 10000;

            /// <summary>
            /// A bee that check for idle time repeatedly. requires sleeping interval in seconds and bears.
            /// </summary>
            public IdleBee(int i, params IBear[] args)
                : base(i, args)
            {
            }

            internal struct LASTINPUTINFO
            {
                public uint cbSize;
                public uint dwTime;
            }

            [DllImport("user32.dll")]
            static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);


            public Boolean IsIdle()
            {
                // Get the system uptime

                int systemUptime = Environment.TickCount;

                // The tick at which the last input was recorded

                int LastInputTicks = 0;

                // The number of ticks that passed since last input

                int IdleTicks = 0;

                LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();

                LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);

                LastInputInfo.dwTime = 0;

                // If we have a value from the function

                if (GetLastInputInfo(ref LastInputInfo))
                {

                    // Get the number of ticks at the point when the last activity was seen

                    LastInputTicks = (int)LastInputInfo.dwTime;

                    // Number of idle ticks = system uptime ticks - number of ticks at last input

                    IdleTicks = systemUptime - LastInputTicks;

                }


                if (IdleTicks > idleCriteria)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }



            public override void StartFlying(object o)
            {
                base.StartFlying(null);
            }

            public override void SuckFlower()
            {
                base.SuckFlower();

                string s;
                Boolean isIdle;

                isIdle = IsIdle();
                if (isIdle)
                {
                    s = "Is Idle...";
                }
                else
                {
                    s = "Not Idle!";
                }

                infoTable["Comment"] = s;
                infoTable["Idle"] = isIdle;
            }

        }

        public class ForegroundBee : AbstractBee, IIdleSensitive
        {
            /// <summary>
            /// A bee that check for idle time repeatedly. requires sleeping interval in seconds and bears.
            /// </summary>
            public ForegroundBee(int i, params IBear[] args)
                : base(i, args)
            {
            }

            [DllImport("user32.dll")]
            static extern int GetForegroundWindow();

            [DllImport("user32.dll")]
            static extern int GetWindowText(int hWnd, StringBuilder text, int count);

            [DllImport("user32")]
            public static extern int GetWindowThreadProcessId(int hwnd, ref int lpdwProcessId);


            public override void StartFlying(object o)
            {
                base.StartFlying(null);
            }

            public override void SuckFlower()
            {
                base.SuckFlower();
                
                const int nChars = 256;
                string s, windowLabel, processName = null;
                int handle = 0, processID = 0;
                StringBuilder Buff = new StringBuilder(nChars);

                handle = GetForegroundWindow();

                if (GetWindowText(handle, Buff, nChars) > 0)
                {

                    windowLabel = Buff.ToString();

                    if (GetWindowThreadProcessId(handle, ref processID) > 0)
                    {
                        Process p = Process.GetProcessById(processID);
                        processName = p.ProcessName;
                    }
                    s = "working on " + windowLabel + " / " + processName;
                }
                else
                {
                    windowLabel = "Unknown";
                    processName = "Unknown";
                    s = "Nothing seems to be on the foreground";
                }

                infoTable["Comment"] = s;
                infoTable["ForegroundWindow"] = windowLabel;
                infoTable["ForegroundProcess"] = processName;
            }

        }
    }

}